Loading libraries

# Main
#library(r5r)
#library(rJava)

# 
library(sf)
library(tidyverse)

# Visualization
library(leaflet)
library(tmap)
library(mapview); mapviewOptions(platform = 'leafgl')

Loading Shape Files and Score Data into R

# takes a long time so keep it in a separate cell

# geo data
canada_dbs <- st_read("census_2016_digital_DBS_gml_cartographic/ldb_000b16g_e.gml")
## Reading layer `ldb_000b16g_e' from data source `C:\Users\Luka\Documents\GitHub\Capstone-Project\code\Visualizations\census_2016_digital_DBS_gml_cartographic\ldb_000b16g_e.gml' using driver `GML'
## Simple feature collection with 489676 features and 28 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 3689439 ymin: 659338.9 xmax: 9015737 ymax: 5242179
## Projected CRS: NAD83 / Statistics Canada Lambert
# keep necessary columns and rows
van_dbs <- data.frame(canada_dbs[which(canada_dbs$CMANAME == "Vancouver"), ])
clean_van_dbs <- van_dbs[, c(1, 2, 29)]

# score data
nearest_gallery_scores <- read.csv("../../data/score_sets/nearest1_gallery_scores.csv")
nearest_gallery_scores$fromId <- as.character(nearest_gallery_scores$fromId)

# join
van_dbs_scores <- left_join(clean_van_dbs, nearest_gallery_scores, by = c('DBUID' = 'fromId'))
head(van_dbs_scores)
# convert back to sf object
van_dbs_scores_sf <- st_as_sf(van_dbs_scores)

# plot blocks if curious but below are the cooler visualizations
#plot(van_dbs$geometry)

Visualizing in tmap

tm_shape(van_dbs_scores_sf) +
  tm_polygons("score", 
              style="quantile", 
              title="Vancouver Gallery Transit Accessibility")

tmap_mode("view")
## tmap mode set to interactive viewing
tmap_last()

Visualizing in leaflet

# leaflet requires the sf object be reprojected
van_dbs_scores_st <- st_transform(van_dbs_scores_sf,crs = 4326)

# base map test
leaflet(van_dbs_scores_st) %>%
  addPolygons(stroke = FALSE)
# pretty map test

pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)
p_popup <- paste0("<strong>Accessibility: </strong>", van_dbs_scores_st$score)

leaflet(van_dbs_scores_st) %>%
  addPolygons(
    stroke = FALSE,  # remove polygon borders
    fillColor = ~pal_fun(score), # set fill colour with pallette fxn from aboc
    fillOpacity = 0.7, smoothFactor = 0.5, # aesthetics
    popup = p_popup) %>% # add message popup to each block
  addTiles() %>%
  addLegend("topright",  # location
            pal=pal_fun,    # palette function
            values=~score,  # value to be passed to palette function
            title = "Vancouver Gallery Transit Accessibility") # legend title

NOT IMPORTANT: Visualization of Street Network using ggplot